package com.houxy.service.impl;

import com.houxy.repository.mapper.RegisterMapper;
import com.houxy.repository.mapper.RegisterTempMapper;
import com.houxy.repository.model.bo.RegisterBO;
import com.houxy.repository.model.bo.UserBO;
import com.houxy.repository.model.entity.Register;
import com.houxy.repository.model.entity.RegisterTemp;
import com.houxy.service.ExpertService;
import com.houxy.service.RegisterService;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hou
 * @date 2019/3/18 15:53
 * @describe：
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RegisterTempMapper registerTempMapper;

    @Autowired
    private ExpertService expertService;

    @Override
    public boolean addRegister(Register register) {
        RegisterTemp registerTemp = findByExpertAndTime(register.getExpertId(), register.getCreateTime());
        if (registerTemp == null) {
            RegisterTemp registerTempNew = new RegisterTemp();
            registerTempNew.setExpertId(register.getExpertId());
            registerTempNew.setDate(register.getCreateTime());
            registerTempNew.setNum(3);
            addRegisterTemp(registerTempNew);
            int insert = registerMapper.insertSelective(register);
            return true;
        } else {
            if (registerTemp.getNum() == 0) {
                return false;
            } else {
                registerTemp.setNum(registerTemp.getNum() - 1);
                registerTempMapper.updateByPrimaryKey(registerTemp);
                return true;
            }
        }
    }

    @Override
    public boolean addRegisterTemp(RegisterTemp registerTemp) {
        int insert = registerTempMapper.insert(registerTemp);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public RegisterTemp findByExpertAndTime(int expertId, String date) {
        return registerTempMapper.findByExpertAndTime(expertId, date);
    }

    @Override
    public boolean deleteRegiset(int registerId) {
        int delete = registerMapper.deleteByPrimaryKey(registerId);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<RegisterBO> findRegisterByUser(int userId) {
        List<Register> regiserByUserList = registerMapper.findRegiserByUser(userId);
        List<RegisterBO> registerBOList = new ArrayList<>();
        if (regiserByUserList != null) {
            regiserByUserList.stream().forEach(register -> {
                RegisterBO registerBO = BeanMapper.map(register, RegisterBO.class);
                registerBO.setExpertBO(expertService.findAllById(register.getExpertId()));
                registerBOList.add(registerBO);
            });
        }
        if (registerBOList != null && registerBOList.size() > 0) {
            return registerBOList;
        }
        return null;
    }

    @Override
    public List<RegisterBO> findRegisterByExpert(int expertId) {
        List<Register> regiserByUserList = registerMapper.findRegiserByExpert(expertId);
        List<RegisterBO> registerBOList = new ArrayList<>();
        if (regiserByUserList != null) {
            regiserByUserList.stream().forEach(register -> {
                RegisterBO registerBO = BeanMapper.map(register, RegisterBO.class);
                registerBO.setExpertBO(expertService.findAllById(register.getExpertId()));
                registerBOList.add(registerBO);
            });
        }
        if (registerBOList != null && registerBOList.size() > 0) {
            return registerBOList;
        }
        return null;
    }

    @Override
    public List<UserBO> findUserByExpertId(int expertId) {
        List<Register> registerList = registerMapper.findRegiserByExpert(expertId);
        List<UserBO> userList = new ArrayList<>();
        if (registerList != null && registerList.size() > 0) {
            registerList.stream().forEach(register -> {
                UserBO userBO = new UserBO();
                userBO.setName(register.getName());
                userBO.setDisease(register.getDisease());
                userBO.setDate(register.getCreateTime());
                userBO.setTel(register.getTel());
                userBO.setRegisterId(register.getId());
                userList.add(userBO);

            });
        }
        return userList;
    }

    @Override
    public void update(Integer id) {
        registerMapper.updateStatusById(id);
    }
}
